package work3.work5;

import Uti.ScannerUti;

import java.io.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StudentInfoManagement {
    static ArrayList<Student> list = new ArrayList<>();
    static {
        File file = new File("./Student.obj");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if(file.length()!=0){
            Object obj = null;
            try {
                FileInputStream fis = new FileInputStream("./Student.obj");
                ObjectInputStream ois = new ObjectInputStream(fis);
                obj = ois.readObject();

            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            list.add((Student) obj);
        }

    }

    public StudentInfoManagement() {
        System.out.println("学生信息管理");
        System.out.println("---------------------------");
        while (true) {
            System.out.print("学生信息添加——1\n学生信息修改——2\n学生信息删除——3\n学生信息查询——4\n退出——0\n");
            int kk = ScannerUti.getInt();
            if (kk == 1) {
                add();
            } else if (kk == 2) {
                change();
            } else if (kk == 3) {
                delete();
            }else if (kk == 4) {
                find();
            } else if (kk == 0) {
                break;
            }
            try {
                toFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

    }

    void delete() {
        print();
        System.out.println("请输入要删除的学生的编号，按n结束");
        String s = ScannerUti.getSting();
        if (s.equals("n")) return;
        int student = findStudent(s);
        if (student > -1) {
            list.remove(student);
            System.out.println("删除成功");
            print();
        } else {
            System.out.println("该学生不存在");
        }
    }

    private void change() {
        print();
        while (true) {
            System.out.println("请输入要修改的学生的编号,按0退出");
            String s = ScannerUti.getSting();
            if(s.equals("0"))break;
            int i = findStudent(s);
            if (i == -1) {
                System.out.println("该学生不存在");
                continue;
            }
            Student student = list.get(i);
            if (student.Id.equals(s)) {
                System.out.println("请输入需要修改的信息 1-姓名 2-年龄 3-全部");
                int kk = ScannerUti.getInt();
                if (kk == 1) {
                    System.out.println("请输入姓名");
                    student.setName(ScannerUti.getSting());
                } else if (kk == 2) {
                    System.out.println("请输入年龄");
                    student.setAge(ScannerUti.getInt());
                } else {
                    System.out.println("请输入姓名 年龄 以空格隔开");
                    String s1 = ScannerUti.getSting();
                    String[] split = s1.split(" ");
                    if (split.length == 2) {
                        student.setName(split[0]);
                        try {
                            student.setAge(Integer.parseInt(split[1]));
                        } catch (NumberFormatException e) {
                            System.out.println("输入有误");
                            continue;
                        }
                    }

                }

                break;
            }
        }
    }

    void add() {

        System.out.println("请输入学生 ID 姓名 年龄 以空格隔开,输入 n结束");
        while (true) {
            Student student = new Student();
            String s = ScannerUti.getSting();
            if (s.equals("n")) break;

            String[] split = s.split(" ");
            if(split.length!=3){
                System.out.println("输入有误");
                continue;
            }
            student.Id = split[0];
            student.name =split[1];
            try {
                student.age =Integer.parseInt(split[2]);
                if(student.age<0||student.age>150){
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException e) {
                System.out.println("输入有误");
                System.out.println("请输入学生 ID 姓名 年龄 以空格隔开");
                continue;
            }
            list.add(student);
        }

    }

    void print() {
        for (Student s : list) {
            System.out.println(s);
        }
    }

    public int findStudent(String id) {
        int index = -1;
        for (Student s : list) {
            if (s.Id.equals(id)) {
                index = list.indexOf(s);
            }
        }
        return index;
    }
    public void find() {//模糊匹配
        System.out.println("请输入你想查找的学生的任何信息（任意一个信息即可，可不完整）");
        String someString = ScannerUti.getSting();
        Pattern pattern = Pattern.compile(".*"+someString+".*");
        for (Student s : list) {
            String all = s.Id+s.age+s.name;
            Matcher matcher = pattern.matcher(all);
            Boolean bool=false;
            while (matcher.find()){
                bool=true;
                break;
            }
            if(bool){
                System.out.println(s.toString());
            }

        }

    }

    void toFile() throws IOException {
        FileOutputStream fos = new FileOutputStream("./Student.obj");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        for(Student s:list){
            oos.writeObject(s);
        }
        oos.close();
    }



}
